package com.lx.spring.aop;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

import java.util.Arrays;

@Component
@Aspect
public class MyAspect {

            //切点
    @Before("execution(* com.lx..*Mapper.*(..))")
    //直接用方法参数注入对象=>接口注入
    //JoinPoint 连接点
    public void before(JoinPoint jp) {
        System.out.println("方法签名 jp.getSignature() = " + jp.getSignature());
        System.out.println("参数数组 jp.getArgs() = " + Arrays.toString(jp.getArgs()));
        System.out.println("------前置拦截--------");
    }

    //切点
    @Pointcut("execution(* com.lx..*Mapper.*(..))")
    public void myPointcut() {}

    @After("myPointcut()")
    public void after(JoinPoint jp) {
        System.out.println("------后置拦截--------");
    }

    @AfterReturning(value = "myPointcut()", returning = "ret")
    public void afterReturning(JoinPoint jp, Object ret) {
        //可以获取业务的返回值
        System.out.println("业务的返回值 ret = " + ret);
        System.out.println("------返回拦截--------");
    }


    @AfterThrowing(value = "myPointcut()", throwing = "e")
    public void afterThrowing(JoinPoint jp, Throwable e) {
        System.out.println("业务方法的异常 e = " + e);
        System.out.println("------异常拦截--------");
    }

@Around("myPointcut()")
    public Object around(ProceedingJoinPoint pjp){
        try{
            System.out.println("-------------环绕增强:前置------------------");
            Object ret = pjp.proceed();
            System.out.println("-------------环绕增强:返回------------------");
            return ret;
        }catch (Throwable e){
            System.out.println("-------------环绕增强:异常------------------");
            throw new RuntimeException("业务方法异常", e);
        }finally {
            System.out.println("-------------环绕增强:后置------------------");
        }
    }


    /*
    *try{
    *前置增强
    *
    * 业务方法
    *
    * 返回增强
    *
    * }catch(Exception e){
    *
    * 异常增强
    *
    * }finally{
    *
    *
    * 后置增强
    *
    * }
    *
    *
    * */

}
